package com.five.admin.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.admin.dto.DeliveryOrderDTO;
import com.five.admin.dto.OrderQueryDto;
import com.five.admin.mapper.OrderMapper;
import com.five.admin.mapper.UserMapper;
import com.five.admin.server.OrderService;
import com.five.admin.server.UserOrderService;
import com.five.admin.vo.OrderListVo;
import com.five.core.enums.OrderStatusEnum;
import com.five.core.exception.CustomerException;
import com.five.core.result.ResultEnum;
import com.five.pojo.entity.Order;
import com.five.pojo.entity.User;
import com.five.pojo.vo.OrderDetailVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserOrderService userOrderService;

    @Override
    public Page<OrderListVo> list(OrderQueryDto queryDto) {
        LocalDateTime dateTime = rangeDate(queryDto.getDateRange());
        Page<Order> page = new Page<>(queryDto.getPage(), queryDto.getLimit());
        queryDto.setStartTime(dateTime);
        Page<OrderListVo> orderPage = getBaseMapper().list(page, queryDto);
        return orderPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public boolean sendOrder(Long orderId) {
        Order order = getById(orderId);
        if (order != null) {
            order.setOrderStatus(2);
            order.setDeliveryTime(LocalDateTime.now());
            return updateById(order);
        }
        return false;
    }

    @Override
    public OrderDetailVO getOrderDetail(Long orderId) {

        return userOrderService.getOrderDetail(orderId);
    }

    @Override
    public boolean delivery(DeliveryOrderDTO deliveryOrderDTO) {

        Order order = getById(deliveryOrderDTO.getOrderId());
        if (order == null) {
            throw new CustomerException(ResultEnum.ORDER_NOT_EXIST);
        }

        if (!OrderStatusEnum.UNDELIVERED.getCode().equals(order.getOrderStatus())) {
            throw new CustomerException(ResultEnum.ORDER_STATUS_NOT_ALLOW_CANCEL.getMessage());
        }
        order.setRemark(deliveryOrderDTO.getExpressRemark());
        order.setExpressNo(deliveryOrderDTO.getExpressNo());
        order.setExpressCompany(deliveryOrderDTO.getExpressCompany());
        order.setOrderStatus(OrderStatusEnum.UNRECEIVED.getCode());
        order.setDeliveryTime(LocalDateTime.now());

        return userOrderService.deliverOrder(order);
    }

    @Override
    public boolean complete(Long orderId) {
        return userOrderService.confirmReceive(orderId);
    }


    /**
     * 根据值，返回对应的时间节点
     *
     * @param dateRange
     * @return
     */
    private LocalDateTime rangeDate(Integer dateRange) {
        LocalDateTime now = LocalDateTime.now();
        if (dateRange == 0) {
            return now.minusDays(30);
        } else if (dateRange == 1) {
            return now.minusDays(90);
        } else if (dateRange == 2) {
            return now.minusMonths(6);
        } else return now.minusYears(1);
    }

    /**
     * 根据日期范围获取查询的起始时间
     * 0 - 30天以内
     * 1 - 90天以内
     * 2 - 一年以内
     * 3 - 一年以上
     */
    private LocalDateTime getRangeStartTime(Integer dateRange) {
        LocalDateTime now = LocalDateTime.now();
        if (dateRange == null) {
            return now.minusDays(30); // 默认30天
        }

        switch (dateRange) {
            case 0:
                return now.minusDays(30);   // 30天以内：从30天前到现在
            case 1:
                return now.minusDays(90);   // 90天以内：从90天前到现在
            case 2:
                return now.minusYears(1);   // 一年以内：从一年前到现在
            case 3:
                return now.minusYears(1); // 一年以上：设置一个很远的过去时间
            default:
                return now.minusDays(30);  // 默认30天
        }
    }

}
